﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ApplLogic.ApplClasses;

namespace TCPClient.UI
{
    public partial class ShiftStatForm : Form
    {
        private int m_NumberOfWanted = 0;
        private int m_NumberOfForbidden = 0;
        private int m_NumberOfFirstStep = 0;
        private int m_NumberOfSecondStep = 0;
        private int m_NumberOfThirdStep = 0;
        private int m_NumberOfManagerStep = 0;
        private ApplShift m_ShiftRef = null;
        private int m_TotalWorkersInshift = 0;

        public ShiftStatForm(ApplShift shiftToShowStat, int wanted, int forb, int first, int second, int third, int manager)
        {
            setParams(wanted, forb, first, second, third, manager);
            m_ShiftRef = shiftToShowStat;
            InitializeComponent();
            init();
        }

        private void setParams(int wanted, int forb, int first, int second, int third, int manager)
        {
            m_NumberOfWanted = wanted;
            m_NumberOfForbidden = forb;
            m_NumberOfThirdStep = third;
            m_NumberOfManagerStep = manager;
            m_NumberOfSecondStep = second;
            m_NumberOfFirstStep = first;
        }

        private void init()
        {
            m_TotalWorkersInshift = m_NumberOfFirstStep + m_NumberOfSecondStep + m_NumberOfThirdStep + m_NumberOfManagerStep;
            initTitle();
            initTextBoxes();
            initRequests();
            initRegistrations();
        }

        private void initTextBoxes()
        {
            textBoxByManager.Text = m_NumberOfManagerStep.ToString();
            textBoxByManagerPrecent.Text = string.Format("{0:0.##}%", convertToPrecent(m_NumberOfManagerStep));

            textBoxFirstStep.Text = m_NumberOfFirstStep.ToString();
            textBoxFirstStepPrecent.Text = string.Format("{0:0.##}%", convertToPrecent(m_NumberOfFirstStep));

            textBoxSecondStep.Text = m_NumberOfSecondStep.ToString();
            textBoxSecondStepPrecent.Text = string.Format("{0:0.##}%", convertToPrecent(m_NumberOfSecondStep));

            textBoxThirdStep.Text = m_NumberOfThirdStep.ToString();
            textBoxThirdStepPrecent.Text = string.Format("{0:0.##}%", convertToPrecent(m_NumberOfThirdStep));

            textBoxWanted.Text = m_NumberOfWanted.ToString();
            textBoxForbidden.Text = m_NumberOfForbidden.ToString();

            textBoxTotal.Text = m_TotalWorkersInshift.ToString();
            textBoxTotalPrecent.Text = @"100%";

        }

        private void initRegistrations()
        {
            chartRegistrations.ChartAreas[0].Axes[1].Maximum = 100;
            chartRegistrations.Series[0].Points[0].YValues = new[] { convertToPrecent(m_NumberOfFirstStep) };
            chartRegistrations.Series[0].Points[0].Label = string.Format("{0:0.##}%", chartRegistrations.Series[0].Points[0].YValues[0]);
            chartRegistrations.Series[1].Points[0].YValues = new[] { convertToPrecent(m_NumberOfSecondStep) };
            chartRegistrations.Series[1].Points[0].Label = string.Format("{0:0.##}%", chartRegistrations.Series[1].Points[0].YValues[0]);
            chartRegistrations.Series[2].Points[0].YValues = new[] { convertToPrecent(m_NumberOfThirdStep) };
            chartRegistrations.Series[2].Points[0].Label = string.Format("{0:0.##}%", chartRegistrations.Series[2].Points[0].YValues[0]);
            chartRegistrations.Series[3].Points[0].YValues = new[] { convertToPrecent(m_NumberOfManagerStep) };
            chartRegistrations.Series[3].Points[0].Label = string.Format("{0:0.##}%", chartRegistrations.Series[3].Points[0].YValues[0]);
        }

        private void initRequests()
        {
            chartRequests.ChartAreas[0].Axes[1].Maximum =
                Math.Max(m_NumberOfForbidden, m_NumberOfWanted) + 2;
            chartRequests.Series[0].Points[0].YValues = new[] { (double)m_NumberOfWanted };
            chartRequests.Series[1].Points[0].YValues = new[] { (double)m_NumberOfForbidden };
        }

        private void initTitle()
        {
            this.labelTitle.Text = string.Format("Statistics for Shift {0} ({1} {2} - {3}):", m_ShiftRef.Name,
                                                 m_ShiftRef.BeginTime.ToShortDateString(),
                                                 m_ShiftRef.BeginTime.ToShortTimeString(),
                                                 m_ShiftRef.EndTime.ToShortTimeString());
        }

        private double convertToPrecent(int part)
        {
            return (double)((double)part / (double)m_TotalWorkersInshift) * 100;
        }

        
    }
}
